package com.longcai.piaojia.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.adapter.MyBagAdapter;
import com.longcai.piaojia.conn.Conn;
import com.longcai.piaojia.conn.MyBagListGet;
import com.longcai.piaojia.util.MyToast;
import com.longcai.piaojia.util.ZXingUtils;
import com.module.pull.PullToRefreshBase;
import com.module.pull.PullToRefreshListView;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.http.note.HttpInlet;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/8/2.
 */
public class MyBagActivity extends BaseActivity {

    @BoundView(R.id.mybag_lv) private PullToRefreshListView pullToRefreshListView;
//    @BoundView(R.id.mybag_qr_code_iv) private ImageView mybag_qr_code_iv;

    private SimpleDateFormat mDateFormat = new SimpleDateFormat("MM-dd HH:mm");

    private Bitmap bitmap;

    private ListView mybag_lv;
    private List<MyBagListGet.MyBagEntity> lists = new ArrayList<>();

    private MyBagAdapter adapter;


    private MyBagListGet.MyBagListEntity myBagEntity;

    private MyBagListGet myBagListGet = new MyBagListGet(new AsyCallBack<MyBagListGet.MyBagListEntity>(){
        @Override
        public void onSuccess(String toast, int type, MyBagListGet.MyBagListEntity myBagListEntity) throws Exception {

            myBagEntity = myBagListEntity;

            lists.addAll(myBagListEntity.myBagEntities);

            adapter.notifyDataSetChanged();
        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.show(context, toast, 2000);
        }

        @Override
        public void onEnd(String toast, int type) throws Exception {

            pullToRefreshListView.onPullUpRefreshComplete();
            pullToRefreshListView.onPullDownRefreshComplete();
            setLastUpdateTime();
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_mybag);

        setBackTrue();

        setTitleName(getString(R.string.mybag_title_text));

        pullToRefreshListView.setScrollLoadEnabled(false);
        pullToRefreshListView.setPullLoadEnabled(true);
        pullToRefreshListView.setPullRefreshEnabled(true);

        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                lists.clear();

                myBagListGet.page = 1;
                myBagListGet.user_id = BaseApplication.BasePreferences.readUserId();

                myBagListGet.execute(MyBagActivity.this, false);

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {


                if (lists != null && myBagEntity.total > lists.size()) {

                    myBagListGet.page = myBagEntity.current_page + 1;
                    myBagListGet.execute(MyBagActivity.this, false);

                } else {

                    pullToRefreshListView.onPullUpRefreshComplete();
                    pullToRefreshListView.onPullDownRefreshComplete();

                }

            }
        });

        mybag_lv = pullToRefreshListView.getRefreshableView();



        mybag_lv.setAdapter(adapter = new MyBagAdapter(MyBagActivity.this, lists));


//        mybag_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//
//                startActivity(new Intent(MyBagActivity.this, GoodsInfoActivity.class).putExtra("id", lists.get(position).showlistid));
//
//            }
//        });
//

//        bitmap = ZXingUtils.creatBarcode(this, "137564", ScaleScreenHelperFactory.getInstance().getWidthHeight(330), ScaleScreenHelperFactory.getInstance().getWidthHeight(200), false);
//
//        mybag_qr_code_iv.setImageBitmap(bitmap);

        myBagListGet.page = 1;

        myBagListGet.user_id = BaseApplication.BasePreferences.readUserId();

        myBagListGet.execute(MyBagActivity.this);


    }

    private void setLastUpdateTime() {
        String text = formatDateTime(System.currentTimeMillis());
        pullToRefreshListView.setLastUpdatedLabel(text);
    }

    private String formatDateTime(long time) {
        if (0 == time) {
            return "";
        }

        return mDateFormat.format(new Date(time));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (bitmap != null) {

            bitmap.recycle();
            bitmap = null;

        }
    }



}
